package com.wootion.cims.general.utils;



import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@Service
public class FileService {

	private final Path fileStorageLocation; // 文件在本地存储的地址
	
	public static final String REGEX_ID_CARD18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$";

	@Autowired
	public FileService(FileProperties fileProperties) {
		this.fileStorageLocation = Paths.get(fileProperties.getUploadDir()).toAbsolutePath().normalize();
		try {
			Files.createDirectories(this.fileStorageLocation);
		} catch (Exception ex) {
			throw new FileException("Could not create the directory where the uploaded files will be stored.", ex);
		}
	}

	/**
	 * 加载文件
	 * 
	 * @param fileName 文件
	 * @return 文件
	 */
	public Resource loadFileAsResource(String fileName) {
		try {
			Path filePath = this.fileStorageLocation.resolve(fileName).normalize();
			Resource resource = new UrlResource(filePath.toUri());
			if (resource.exists()) {
				return resource;
			} else {
				throw new FileException("File not found " + fileName);
			}
		} catch (MalformedURLException ex) {
			throw new FileException("File not found " + fileName, ex);
		}
	}

	 public static boolean isIDCard18(final CharSequence input) {
//	        return Pattern.matches(REGEX_ID_CARD18, input);
		 return input.length()==18;
	  }
}
